package com.lcy.demo.client;

import com.google.gson.Gson;
import com.lcy.demo.User;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.*;
import okio.BufferedSink;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;

/**
 * <p>创建时间：2017/4/17 11:26</p>
 *
 * @author 娄存银
 * @version 1.0
 */
public class Client {
    public static OkHttpClient getClient(){
        return new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        HttpUrl url = request.url();
                        System.out.println("request url : "+url);
                        return chain.proceed(chain.request());
                    }
                })
                .build();
    }

    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:8080/")
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        LocalTestService service = retrofit.create(LocalTestService.class);
        Observer<User> observer = new Observer<User>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(User user) {
                System.out.println(user);
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        };
        service.getUser(1).subscribe(observer);
        User user = new User();
        user.setName("hh");
        user.setAge(1);
        user.setId(2);
        service.getObject(user).subscribe(observer);
    }
}
